package org.yefei.homeworkday25;

import java.io.*;

public class Two {

	public static void main(String[] args) {
		// 2：附件 chaoxian.txt是个文本文件，用程序把其中的金正恩替换成 金三胖，
		// 提示：把内容取出来，替换后，写入文件。
		InputStream i = null;
		Reader re = null;
		BufferedReader r = null;
		Writer wr = null, w = null;
		try {
			// 读数据
			i = new FileInputStream("F:\\叶飞io测试\\IOhomework测试\\chaoxian.txt");
			re = new InputStreamReader(i, "gbk");
			r = new BufferedReader(re);
			String line = null;
			StringBuilder s = new StringBuilder();
			while ((line = r.readLine()) != null) {// 按行读
				s.append(line);
			}
			System.out.println("替换前：\n\n" + s);
			// 开始替换：
			String replaceAll = s.toString().replaceAll("正恩", "三胖");
			System.out.println("替换后：\n\n" + replaceAll);
			wr = new FileWriter("F:\\叶飞io测试\\IOhomework测试\\chaoxian.txt");
			w = new BufferedWriter(wr);
			w.write(replaceAll);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (w != null)
				try {
					w.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (wr != null)
				try {
					wr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (r != null)
				try {
					r.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (re != null)
				try {
					re.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if (i != null)
				try {
					i.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
}
